package com.wanqing.controller.admin;

import com.wanqing.dto.BookListDTO;
import com.wanqing.dto.BookListPageQueryDTO;
import com.wanqing.result.PageResult;
import com.wanqing.result.Result;
import com.wanqing.service.BookListService;
import com.wanqing.vo.BookListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 书单相关接口的控制器类
 */
@RestController
@RequestMapping("/admin/bookList")
@Slf4j
@Tag(name = "书单相关接口")
public class BookListController {

    @Resource
    private BookListService bookListService;

    /**
     * 书单分页查询接口
     *
     * @param bookListPageQueryDTO 分页查询的条件封装对象
     * @return 返回分页结果
     */
    @GetMapping("/page")
    @Operation(summary = "书单分页查询")
    public Result<PageResult> pageQuery(BookListPageQueryDTO bookListPageQueryDTO) {
        log.info("书单分页查询：{}", bookListPageQueryDTO);
        PageResult pageResult = bookListService.pageQuery(bookListPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增书单接口
     *
     * @param bookListDTO 新增书单的DTO对象
     * @return 返回新增结果
     */
    @PostMapping
    @Operation(summary = "新增书单")
    @CacheEvict(cacheNames = "bookListCache", key = "#bookListDTO.categoryId") //key: bookListCache::categoryId
    public Result<String> save(@RequestBody BookListDTO bookListDTO) {
        log.info("新增书单：{}", bookListDTO);
        bookListService.saveWithBook(bookListDTO);
        return Result.success();
    }

    /**
     * 根据ID查询书单接口
     *
     * @param id 书单的ID
     * @return 返回查询到的书单VO对象
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询书单")
    public Result<BookListVO> getById(@PathVariable Long id) {
        BookListVO BookListVO = bookListService.getByIdWithBook(id);
        return Result.success(BookListVO);
    }

    /**
     * 编辑书单接口
     *
     * @param bookListDTO 编辑后的书单DTO对象
     * @return 返回编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑书单")
    @CacheEvict(cacheNames = "bookListCache", allEntries = true)
    public Result<String> update(@RequestBody BookListDTO bookListDTO) {
        log.info("编辑书单：{}", bookListDTO);
        bookListService.updateWithBook(bookListDTO);
        return Result.success();
    }

    /**
     * 启用或停售书单接口
     *
     * @param status 书单的状态，0为停售，1为启用
     * @param id     书单的ID
     * @return 返回操作结果
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "启用停售书单")
    @CacheEvict(cacheNames = "bookListCache", allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        bookListService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 批量删除书单接口
     *
     * @param ids 待删除的书单ID列表
     * @return 返回删除结果
     */
    @DeleteMapping
    @Operation(summary = "批量删除书单")
    @CacheEvict(cacheNames = "bookListCache", allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids) {
        bookListService.deleteBatch(ids);
        return Result.success();
    }
}
